package xin.nick.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpRequest;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.nick.demo.config.ServerConfig;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author Nick
 * @date 2021/6/27
 * @description
 */
@RestController
public class LoginController {




    @RequestMapping("/login")
    public void login(String username, String password, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws IOException {

        if ("admin".equals(username) && "admin".equals(password)) {
            System.out.println("----------> 登录成功");
            session.setAttribute("username", username + System.currentTimeMillis());
            response.sendRedirect("main.html");
        } else {
            System.out.println("----------> 登录失败");
            response.sendRedirect("index.html");
        }

    }

    @RequestMapping("/getUsername")
    public String getUsername(HttpSession session) {

        System.out.println("----------> getUsername");
        return (String) session.getAttribute("username");
    }

}
